<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>表单使用 | Python Web 框架要点</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C08-Admin/index.html" />
    
    
    <link rel="prev" href="../C07-Form/index.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="8.1"
        data-chapter-title="表单使用"
        data-filepath="C07-Form/form.md"
        data-basepath=".."
        data-revision="Sat May 05 2018 07:17:09 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Python Web 框架要点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="AboutDjango.html">
            
                
                    <a href="../AboutDjango.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Django介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C01-CreateProject/index.html">
            
                
                    <a href="../C01-CreateProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C01-CreateProject/EnvInstall.html">
            
                
                    <a href="../C01-CreateProject/EnvInstall.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C01-CreateProject/StartProject.html">
            
                
                    <a href="../C01-CreateProject/StartProject.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        创建工程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C01-CreateProject/StartApp.html">
            
                
                    <a href="../C01-CreateProject/StartApp.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        创建子应用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C01-CreateProject/CreateView.html">
            
                
                    <a href="../C01-CreateProject/CreateView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        创建视图
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C02-SettingsStaticAndURL/index.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        配置、静态文件与路由
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C02-SettingsStaticAndURL/Settings.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/Settings.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        配置文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C02-SettingsStaticAndURL/StaticFiles.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/StaticFiles.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        静态文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C02-SettingsStaticAndURL/URL.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/URL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        路由说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="C02-SettingsStaticAndURL/AppConfig.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/AppConfig.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        App应用配置
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C03-RequestAndResponse/index.html">
            
                
                    <a href="../C03-RequestAndResponse/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        请求与响应
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C03-RequestAndResponse/Request.html">
            
                
                    <a href="../C03-RequestAndResponse/Request.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        请求Request
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C03-RequestAndResponse/Response.html">
            
                
                    <a href="../C03-RequestAndResponse/Response.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        响应Response
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C03-RequestAndResponse/Cookie.html">
            
                
                    <a href="../C03-RequestAndResponse/Cookie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Cookie
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C03-RequestAndResponse/Session.html">
            
                
                    <a href="../C03-RequestAndResponse/Session.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        Session
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C04-ClassViewAndMiddleware/index.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        类视图与中间件
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C04-ClassViewAndMiddleware/ClassView.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/ClassView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        类视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C04-ClassViewAndMiddleware/Middleware.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/Middleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        中间件
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C05-Database/index.html">
            
                
                    <a href="../C05-Database/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C05-Database/Config.html">
            
                
                    <a href="../C05-Database/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="C05-Database/Model.html">
            
                
                    <a href="../C05-Database/Model.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        定义模型类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="C05-Database/Tools.html">
            
                
                    <a href="../C05-Database/Tools.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        演示工具使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="C05-Database/CURD.html">
            
                
                    <a href="../C05-Database/CURD.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        数据库操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="C05-Database/QuerySet.html">
            
                
                    <a href="../C05-Database/QuerySet.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        查询集QuerySet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="C05-Database/Manager.html">
            
                
                    <a href="../C05-Database/Manager.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        管理器Manager
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="C06-Template/index.html">
            
                
                    <a href="../C06-Template/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="C06-Template/template.html">
            
                
                    <a href="../C06-Template/template.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        模板使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="C07-Form/index.html">
            
                
                    <a href="../C07-Form/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        表单
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="8.1" data-path="C07-Form/form.html">
            
                
                    <a href="../C07-Form/form.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        表单使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="C08-Admin/index.html">
            
                
                    <a href="../C08-Admin/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        Admin站点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="C08-Admin/Config.html">
            
                
                    <a href="../C08-Admin/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        使用Admin站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="C08-Admin/ModifyListDisplay.html">
            
                
                    <a href="../C08-Admin/ModifyListDisplay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        调整列表页展示
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="C08-Admin/ModifyEditDisplay.html">
            
                
                    <a href="../C08-Admin/ModifyEditDisplay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.3.</b>
                        
                        调整编辑页展示
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.4" data-path="C08-Admin/ModifySite.html">
            
                
                    <a href="../C08-Admin/ModifySite.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.4.</b>
                        
                        调整站点信息
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.5" data-path="C08-Admin/UploadImage.html">
            
                
                    <a href="../C08-Admin/UploadImage.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.5.</b>
                        
                        上传图片
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Python Web 框架要点</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x8868;&#x5355;&#x4F7F;&#x7528;">&#x8868;&#x5355;&#x4F7F;&#x7528;</h1>
<p>Django&#x63D0;&#x4F9B;&#x5BF9;&#x8868;&#x5355;&#x5904;&#x7406;&#x7684;&#x652F;&#x6301;&#xFF0C;&#x53EF;&#x4EE5;&#x7B80;&#x5316;&#x5E76;&#x81EA;&#x52A8;&#x5316;&#x5927;&#x90E8;&#x5206;&#x7684;&#x8868;&#x5355;&#x5904;&#x7406;&#x5DE5;&#x4F5C;&#x3002;</p>
<h2 id="1--&#x5B9A;&#x4E49;&#x8868;&#x5355;&#x7C7B;">1  &#x5B9A;&#x4E49;&#x8868;&#x5355;&#x7C7B;</h2>
<p>&#x8868;&#x5355;&#x7CFB;&#x7EDF;&#x7684;&#x6838;&#x5FC3;&#x90E8;&#x5206;&#x662F;Django &#x7684;Form&#x7C7B;&#x3002; Django &#x7684;&#x6570;&#x636E;&#x5E93;&#x6A21;&#x578B;&#x63CF;&#x8FF0;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x903B;&#x8F91;&#x7ED3;&#x6784;&#x3001;&#x884C;&#x4E3A;&#x4EE5;&#x53CA;&#x5C55;&#x73B0;&#x7ED9;&#x6211;&#x4EEC;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x4E0E;&#x6B64;&#x7C7B;&#x4F3C;&#xFF0C;Form&#x7C7B;&#x63CF;&#x8FF0;&#x4E00;&#x4E2A;&#x8868;&#x5355;&#x5E76;&#x51B3;&#x5B9A;&#x5B83;&#x5982;&#x4F55;&#x5DE5;&#x4F5C;&#x548C;&#x5C55;&#x73B0;&#x3002;</p>
<p>&#x5047;&#x5982;&#x6211;&#x4EEC;&#x60F3;&#x5728;&#x7F51;&#x9875;&#x4E2D;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x8868;&#x5355;&#xFF0C;&#x7528;&#x6765;&#x83B7;&#x53D6;&#x7528;&#x6237;&#x60F3;&#x4FDD;&#x5B58;&#x7684;&#x56FE;&#x4E66;&#x4FE1;&#x606F;&#xFF0C;&#x53EF;&#x80FD;&#x7C7B;&#x4F3C;&#x7684;html &#x8868;&#x5355;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-title">form</span> <span class="hljs-attribute">action</span>=<span class="hljs-value">&quot;&quot;</span> <span class="hljs-attribute">method</span>=<span class="hljs-value">&quot;post&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;text&quot;</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">&quot;title&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;date&quot;</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">&quot;pub_date&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;submit&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">form</span>&gt;</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x636E;&#x6B64;&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;Form&#x7C7B;&#x6765;&#x63CF;&#x8FF0;&#x8FD9;&#x4E2A;&#x8868;&#x5355;&#x3002;</p>
<p>&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;<strong>forms.py</strong>&#x6587;&#x4EF6;&#xFF0C;&#x7F16;&#x5199;Form&#x7C7B;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django <span class="hljs-keyword">import</span> forms

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookForm</span><span class="hljs-params">(forms.Form)</span>:</span>
    title = forms.CharField(label=<span class="hljs-string">&quot;&#x4E66;&#x540D;&quot;</span>, required=<span class="hljs-keyword">True</span>, max_length=<span class="hljs-number">50</span>)
    pub_date = forms.DateField(label=<span class="hljs-string">&apos;&#x51FA;&#x7248;&#x65E5;&#x671F;&apos;</span>, required=<span class="hljs-keyword">True</span>)
</code></pre>
<p>&#x6CE8;&#xFF1A;<a href="https://yiyibooks.cn/xx/Django_1.11.6/ref/forms/fields.html" target="_blank">&#x8868;&#x5355;&#x5B57;&#x6BB5;&#x7C7B;&#x578B;&#x53C2;&#x8003;&#x8D44;&#x6599;&#x8FDE;&#x63A5;</a></p>
<h2 id="2--&#x89C6;&#x56FE;&#x4E2D;&#x4F7F;&#x7528;&#x8868;&#x5355;&#x7C7B;">2  &#x89C6;&#x56FE;&#x4E2D;&#x4F7F;&#x7528;&#x8868;&#x5355;&#x7C7B;</h2>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render
<span class="hljs-keyword">from</span> django.views.generic <span class="hljs-keyword">import</span> View
<span class="hljs-keyword">from</span> django.http <span class="hljs-keyword">import</span> HttpResponse

<span class="hljs-keyword">from</span> .forms <span class="hljs-keyword">import</span> BookForm

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookView</span><span class="hljs-params">(View)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self, request)</span>:</span>
        form = BookForm()
        <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">&apos;book.html&apos;</span>, {<span class="hljs-string">&apos;form&apos;</span>: form})

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self, request)</span>:</span>
        form = BookForm(request.POST)
        <span class="hljs-keyword">if</span> form.is_valid():  <span class="hljs-comment"># &#x9A8C;&#x8BC1;&#x8868;&#x5355;&#x6570;&#x636E;</span>
            print(form.cleaned_data)  <span class="hljs-comment"># &#x83B7;&#x53D6;&#x9A8C;&#x8BC1;&#x540E;&#x7684;&#x8868;&#x5355;&#x6570;&#x636E;</span>
            <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">&quot;OK&quot;</span>)
        <span class="hljs-keyword">else</span>:
            <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">&apos;book.html&apos;</span>, {<span class="hljs-string">&apos;form&apos;</span>: form})
</code></pre>
<ul>
<li>form.is_valid() &#x9A8C;&#x8BC1;&#x8868;&#x5355;&#x6570;&#x636E;&#x7684;&#x5408;&#x6CD5;&#x6027;</li>
<li>form.cleaned_data &#x9A8C;&#x8BC1;&#x901A;&#x8FC7;&#x7684;&#x8868;&#x5355;&#x6570;&#x636E;</li>
</ul>
<h2 id="3-&#x6A21;&#x677F;&#x4E2D;&#x4F7F;&#x7528;&#x8868;&#x5355;&#x7C7B;">3 &#x6A21;&#x677F;&#x4E2D;&#x4F7F;&#x7528;&#x8868;&#x5355;&#x7C7B;</h2>
<pre><code class="lang-html"><span class="hljs-doctype">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">html</span> <span class="hljs-attribute">lang</span>=<span class="hljs-value">&quot;en&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">charset</span>=<span class="hljs-value">&quot;UTF-8&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>&#x4E66;&#x7C4D;<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">form</span> <span class="hljs-attribute">action</span>=<span class="hljs-value">&quot;&quot;</span> <span class="hljs-attribute">method</span>=<span class="hljs-value">&quot;post&quot;</span>&gt;</span>
        {% csrf_token %}
        {{ form }}
        <span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;submit&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">form</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span>
</code></pre>
<ul>
<li>csrf_token   &#x7528;&#x4E8E;&#x6DFB;&#x52A0;CSRF&#x9632;&#x62A4;&#x7684;&#x5B57;&#x6BB5;</li>
<li>form  &#x5FEB;&#x901F;&#x6E32;&#x67D3;&#x8868;&#x5355;&#x5B57;&#x6BB5;&#x7684;&#x65B9;&#x6CD5;</li>
</ul>
<h2 id="4--&#x6A21;&#x578B;&#x7C7B;&#x8868;&#x5355;">4  &#x6A21;&#x578B;&#x7C7B;&#x8868;&#x5355;</h2>
<p>&#x5982;&#x679C;&#x8868;&#x5355;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x4E0E;&#x6A21;&#x578B;&#x7C7B;&#x5BF9;&#x5E94;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x7EE7;&#x627F;<strong>forms.ModelForm</strong>&#x66F4;&#x5FEB;&#x901F;&#x7684;&#x521B;&#x5EFA;&#x8868;&#x5355;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookForm</span><span class="hljs-params">(forms.ModelForm)</span>:</span>
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        model = BookInfo
        fields = (<span class="hljs-string">&apos;btitle&apos;</span>, <span class="hljs-string">&apos;bpub_date&apos;</span>)
</code></pre>
<ul>
<li>model &#x6307;&#x660E;&#x4ECE;&#x5C5E;&#x4E8E;&#x54EA;&#x4E2A;&#x6A21;&#x578B;&#x7C7B;</li>
<li>fields &#x6307;&#x660E;&#x5411;&#x8868;&#x5355;&#x4E2D;&#x6DFB;&#x52A0;&#x6A21;&#x578B;&#x7C7B;&#x7684;&#x54EA;&#x4E2A;&#x5B57;&#x6BB5;</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C07-Form/index.html" class="navigation navigation-prev " aria-label="Previous page: 表单"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C08-Admin/index.html" class="navigation navigation-next " aria-label="Next page: Admin站点"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
